package com.vite.web.controller.common;

import com.vite.captcha.util.CaptchaUtil;
import com.vite.common.config.ViteConfig;
import com.vite.common.constant.CacheConstants;
import com.vite.common.constant.Constants;
import com.vite.common.core.domain.AjaxResult;
import com.vite.common.utils.CacheUtils;
import com.vite.common.utils.uuid.IdUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 验证码操作处理
 */
@RestController
@RequiredArgsConstructor
public class CaptchaController {

    /**
     * EasyCaptcha生成验证码：<a href="https://gitee.com/ele-admin/EasyCaptcha?_from=gitee_search"/>
     */
    @GetMapping("/captchaImage")
    public AjaxResult captchaImage(){
        AjaxResult ajax = AjaxResult.success();
        CaptchaUtil.CaptchaInfo captchaInfo = CaptchaUtil.getImageToBase64(ViteConfig.getCaptchaType());
        //获得验证码信息
        String verifyCode = captchaInfo.getVerifyCode();
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        CacheUtils.put(CacheConstants.CAPTCHA_CODE_KEY, uuid, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        ajax.put("uuid", uuid);
        ajax.put("img", captchaInfo.getImg());
        return ajax;
    }

}
